package demo7;

/**
 * 二叉树中的深搜_求根节点到叶节点数字之和
 * https://leetcode.cn/problems/3Etpl5/
 */
class Solution1 {
    public int sumNumbers(TreeNode root) {
        return dfs(root,0);
    }

    private int dfs(TreeNode root, int num) {
        // 出口
        if(root.left == null && root.right == null) return num * 10 + root.val;

        int left = 0, right = 0;
        if(root.left != null) left = dfs(root.left, num * 10 + root.val);
        if(root.right != null) right = dfs(root.right, num * 10 + root.val);
        return (left + right);
    }
}